package com.etrans.web.gps.business.system.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.system.domain.RoleVO;
import com.etrans.web.gps.business.system.service.RoleService;
import com.etrans.web.gps.business.system.service.UserService;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.common.page.PageResultVo;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.grid.FlexiGridUtil;
import com.etrans.web.gps.util.grid.ParamMap;
import com.etrans.web.gps.util.grid.ParamValueObject;

/**
 * 角色管理
 * @author jw.chen
 *
 */
@Controller
@RequestMapping("/system/role/*")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserService userService;

	// 加载列表
	@RequestMapping(value = "listRoles.html")
	public ModelAndView list(HttpServletRequest request, Model model) {
		ModelAndView mnv = new ModelAndView();
		mnv.addObject("beanForm", new RoleVO());
		mnv.setViewName("views/business/system/role/queryRole");
		return mnv;
	}

	// 查询
	@RequestMapping(value = "loadAllRoles.html")
	public void loadAllRoles(HttpServletRequest request,HttpServletResponse response, String paramsGrid) {
		try {
			ParamMap param = FlexiGridUtil.parseJSONParam(paramsGrid);
			ParamValueObject inputParam = this.parseJSONParam(param);
			HashMap<String, Object> params=inputParam.getMapParams();
			List<RoleVO> rows = roleService.queryAllRole(params);
			Long total = roleService.allRoleCount(params);

			PageVo pageBean = new PageVo();
			pageBean.setRows(rows);
			pageBean.setPage(inputParam.getFromNumber());
			pageBean.setTotal(total.intValue());
			ajaxResultJson(response, pageBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 新增
	@RequestMapping(value = "init.html")
	public ModelAndView init() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("addRole", new RoleVO());
		mav.setViewName("views/business/system/role/addRoleForm");
		return mav;
	}

	//修改用户信息
	@RequestMapping(value = "loadRoleById.html")
	public ModelAndView loadRoleById(HttpServletRequest request, int roleId) {
		ModelAndView mav = new ModelAndView();
		try {
			RoleVO role = roleService.queryRoleByRoleId(roleId);
			//查询角色对应的功能菜单
			String menuIds = roleService.queryRoleMenu(roleId);
			role.setMenuIds(menuIds);
			
			mav.addObject("addRole", role);
			mav.setViewName("views/business/system/role/addRoleForm");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
		
	/**
	 * 新增角色
	 * @param response
	 * @param datas
	 */
	@RequestMapping(value = "saveRole.html")
	public void saveRole(HttpServletRequest request,HttpServletResponse response, RoleVO role) {
		PageResultVo pr = new PageResultVo();
		try {
			int roleCount = roleService.roleNameCount(role);
			if(roleCount == 0){
				role.setCreate_time(new Date());
				role.setEnable(true);
				roleService.saveRole(role);
				//保存角色菜单中间表
				roleService.saveRoleMenu(role);
				
				pr.setCode(Constant.RETURN_OK_CODE);
			}else{
				pr.setCode(Constant.RETURN_Repent_CODE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("保存失败!");
		}
		ajaxResultJson(response, pr);
	}
	
	/**
	 * 修改角色
	 * @param response
	 * @param datas
	 */
	@RequestMapping(value = "updateRole.html")
	public void updateRole(HttpServletRequest request,HttpServletResponse response, RoleVO role) {
		PageResultVo pr = new PageResultVo();
		try {
			int userCount = roleService.roleNameCount(role);
			if(userCount == 0){
				roleService.updateRole(role);
				//保存角色菜单中间表
				roleService.saveRoleMenu(role);
				
				pr.setCode(Constant.RETURN_OK_CODE);
			}else{
				pr.setCode(Constant.RETURN_Repent_CODE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("保存失败!");
		}
		ajaxResultJson(response, pr);
	}

	// 删除信息
	@RequestMapping(value = "deleteRole.html")
	public void deleteRole(HttpServletResponse response, String ids) {
		PageResultVo pr = new PageResultVo();
		try {
			if (ids != null && !"".equals(ids)) {
				String[] id = ids.split(",");
				for (String s : id) {
					roleService.deleteRole(Integer.parseInt(s));
					roleService.deleteRoleMenu(Integer.parseInt(s));
				}
			}
			pr.setCode(Constant.RETURN_OK_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("删除失败!");
		}

		ajaxResultJson(response, pr);
	}
	
	
	//所属角色下拉框
	@RequestMapping(value = "loadRolesSelect.html")
	public void loadRolesSelect(HttpServletRequest request,HttpServletResponse response, String paramsGrid) {
		try {
			List<HashMap<String,Object>> list = roleService.queryRolesSelect();
			ajaxResultJson(response, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
